/*
 * Copyright (c) 2010 Scott Morgan
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package captaintest;

import captaintest.util.Assert;

import java.util.ArrayList;
import java.util.List;

public class FileCoverage implements Comparable<FileCoverage> {
    private String fileName;
    private List<Coverage> coverageList = new ArrayList<Coverage>();

    public FileCoverage(String fileName) {
        this.fileName = fileName;
    }

    public void addCoverage(Coverage coverage) {
        coverageList.add(coverage);
    }

    public String getFileName() {
        return fileName;
    }

    public List<Coverage> getCoverageList() {
        return coverageList;
    }

    public void mergeFileCoverage(FileCoverage latest){
        Assert.isTrue(getFileName().equals(latest.getFileName()));
        Assert.isTrue(getCoverageList().size() == latest.getCoverageList().size());

        for (int i = 0; i < getCoverageList().size(); i++) {
            Coverage oc = getCoverageList().get(i);
            Coverage lc = latest.getCoverageList().get(i);
            oc.mergeCoverage(lc);
        }
        
    }

    public int compareTo(FileCoverage o) {
        return getFileName().compareTo(o.getFileName());
    }

    public boolean hasError(String coverageType){
        for (Coverage coverage : coverageList) {
            if (coverageType == null || coverageType.equals(coverage.getType())){
                if (coverage.hasError()){
                    return true;
                }
            }
        }
        return false;
    }
}
